//
//  SheetView.m
//  HYApp
//
//  Created by Jianbo Cai on 2020/8/12.
//  Copyright © 2020 Unidax. All rights reserved.
//

#import "SheetView.h"

@interface SheetView ()

@property (weak, nonatomic) IBOutlet UIView *actionBgView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentHeight;

@end

@implementation SheetView

- (void)setTitleArray:(NSArray *)titleArray {
    _titleArray = titleArray;
    
    [self.actionBgView.subviews enumerateObjectsUsingBlock:^(__kindof UIView * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [obj removeFromSuperview];
    }];
    
    for (int i = 0; i < titleArray.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 50 * i, SCREEN_WIDTH, 50);
        button.tag = 1000+i;
        button.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:16];
        [button setTitle:titleArray[i] forState:UIControlStateNormal];
        [button setTitleColor:UIColorFromHex(0x333333) forState:UIControlStateSelected];
        [button setTitleColor:UIColorFromHex(0x9A9A9A) forState:UIControlStateNormal];
        [button addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
        [self.actionBgView addSubview:button];
        button.selected = (i == self.index);
        
        UIView *space = [[UIView alloc]initWithFrame:CGRectMake(0, 49, SCREEN_WIDTH, 1)];
        space.backgroundColor = UIColorFromHex(0xE5E5E5);
        [button addSubview:space];
    }
    
    self.contentHeight.constant = titleArray.count * 50;
}

- (void)action:(UIButton *)sender {
    if (self.selectAction) {
        self.selectAction(sender.tag - 1000);
    }
    [self removeFromSuperview];
}

- (IBAction)cancleAction:(id)sender {
    [self removeFromSuperview];
}


@end
